package cn.bdqn.witcity.controller;

import cn.bdqn.witcity.entity.Role;
import cn.bdqn.witcity.entity.User;
import cn.bdqn.witcity.entity.vo.UserVo;
import cn.bdqn.witcity.service.RoleService;
import cn.bdqn.witcity.service.UserService;
import cn.bdqn.witcity.util.ResponseResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UserController {
    @Resource
    UserService userService;

    @Resource
    RoleService roleService;

    @RequestMapping("/sys/user")
    @ResponseBody
    public ResponseResult getUser(UserVo userVo,Integer page,Integer limit){
        if(page==null){
            page=1;
        }
        if(limit==null){
            limit=10;
        }
        userVo.setPage((page-1)*limit);
        userVo.setLimit(limit);
        int count=userService.getUserCount(userVo);
        List<User> users=userService.getUserByPage(userVo);
        return new ResponseResult(0,"",users,count);
    }
    @RequestMapping("/sys/userform")
    public String toAddUser(Model model){
        Role role=new Role();
        List<Role> roles=roleService.getAllRole(role);
        model.addAttribute("roles",roles);
        return "/views/user/user/useradd";
    }
    @RequestMapping("/saveUser")
    @ResponseBody
    public ResponseResult addUser(User user,Integer type){
        int result=userService.addUser(user,type);
        if(result==2){
            return new ResponseResult(200,"",result);
        }else{
            return new ResponseResult(0,"",result);
        }

    }
    @RequestMapping("/updateUser")
    @ResponseBody
    public ResponseResult updateUser(User user){
        int result=userService.updateUser(user);
        if(result==1){
            return new ResponseResult(200,"",result);
        }
        return  new ResponseResult(0,"",result);
    }
    @RequestMapping("/delUser")
    @ResponseBody
    public ResponseResult delUser(Integer id){
        int result=userService.delUser(id);
        if(result>=2){
            return new ResponseResult(200,"",result);
        }
        return new ResponseResult(0,"",result);
    }

}
